package com.winning.vsts.merge.service.configuration;

import com.google.common.eventbus.EventBus;
import com.winning.vsts.merge.service.event.WorksItemEvent;
import com.winning.vsts.merge.service.service.VstsClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * vsts 配置信息
 *
 * @author xcl <xcl@winning.com.cn>
 * @date 2021/11/17
 */
@Configuration
public class VstsAutoConfiguration {


    @Bean
    public VstsContext vstsContext() {
        return new VstsContext();
    }


    @Bean
    public VstsClient vstsClient() {
        return new VstsClient();
    }

    @PostConstruct
    public void registerEvent() {
        EventBus eventBus = eventBus();
        eventBus.register(workItemEvent());
    }

    @Bean
    public EventBus eventBus() {
        return new EventBus();
    }


    @Bean
    public WorksItemEvent workItemEvent() {
        return new WorksItemEvent(vstsContext(), vstsClient());
    }

    @PreDestroy
    public void destroy() {
        eventBus().unregister(workItemEvent());
    }


}
